package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.manager.AdAutoTouchManager;

/**
 * 通用弹框
 */
public class CommonPopup extends CenterPopupView {

    private TextView tvSum, tvTitle, btnTitle, tvUnit, tvbTitle;
    private ImageView imgClose;
    private FrameLayout advert_container;
    private String title, subTitle, value, uiType;
    private boolean isHideClose = false;
    private OnPopupListener mListener;
    private View view;

    public CommonPopup(@NonNull Activity context, String title, String subTitle, String value, String uiType) {
        super(context);
        this.title = title;
        this.subTitle = subTitle;
        this.value = value;
        this.uiType = uiType;
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    public void setHideClose(boolean isHideClose) {
        this.isHideClose = isHideClose;
    }

    public void addAdvertView(View view) {
        this.view = view;
        if (!isHideClose) {
            imgClose.setVisibility(VISIBLE);
        }else{
            if(PopupUtil.isPopupEnd(uiType, subTitle)){
                imgClose.setVisibility(View.VISIBLE);
            }
        }
        advert_container.removeAllViews();
        advert_container.addView(view);
        if ("0".equals(uiType)) {
            btnTitle.setVisibility(VISIBLE);
        }
    }

    public void loadAdvertFail() {
        if (!isHideClose) {
            imgClose.setVisibility(VISIBLE);
        }else{
            if(PopupUtil.isPopupEnd(uiType, subTitle)){
                imgClose.setVisibility(View.VISIBLE);
            }
        }
        if ("0".equals(uiType)) {
            btnTitle.setVisibility(VISIBLE);
        }
        advert_container.setVisibility(GONE);
    }

    public void close() {
        dismiss();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.new_task_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
        setupData();
    }

    private void setupView() {
        tvTitle = findViewById(R.id.tvTitle);
        tvSum = findViewById(R.id.tvSum);
        btnTitle = findViewById(R.id.btTitle);
        tvUnit = findViewById(R.id.tvUnit);
        imgClose = findViewById(R.id.img_close);
        tvbTitle = findViewById(R.id.tvbTitle);
        advert_container = findViewById(R.id.advert_container);

        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                dismiss();
                if (mListener != null) {
                    mListener.onNext();
                }
            }
        });
//        if (Store.getInstance().getTP_AD()) {
//            findViewById(R.id.flCancel).setOnClickListener(v -> {
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//        } else {
//            btnTitle.setOnClickListener(v -> {
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//        }

        imgClose.setOnClickListener(v -> {
            dismiss();
            if (mListener != null) {
                mListener.onClose();
            }
        });
    }

    private void setupData() {
        String unit = "新客专享礼包".equals(title) || "领取成功!".equals(title) ? "元" : "金豆";

        tvUnit.setText(unit);
        tvTitle.setText(title);
        tvSum.setText(value);
        if ("0".equals(uiType)) {
//            btnTitle.setVisibility(VISIBLE);
            btnTitle.setText(subTitle);
        } else {
            tvbTitle.setVisibility(VISIBLE);
            tvbTitle.setText(subTitle);
        }
    }

}

